﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace ReportMobile.admin.inc
{
    /// <summary>
    /// IncAdmin 的摘要说明
    /// </summary>
    public class IncAdmin : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            string jsonStr = "";
            UserInfo user1 = new UserInfo();
            ReportMobile.inc.Common cm = new ReportMobile.inc.Common();
            if (context.Request.QueryString["type"] != null)
            {
                user1.Name = context.Request.Form["fmUserName"].ToString().Trim();
                user1.Variety = context.Request.Form["fmVariety"].ToString().Trim();
                user1.UnitId = context.Request.Form["fmUnitId"].ToString().Trim();
                user1.UnitCustom = context.Request.Form["fmUnitCustom"].ToString().Trim();
                StringBuilder sb = new StringBuilder();
                if (context.Request.QueryString["type"] == "add")
                {
                    user1.OaId = context.Request.Form["fmoaid"].ToString().Trim();
                    if (user1.OaId == "" || user1.Name == "") {
                        jsonStr = "{\"err\":-1,\"msg\":\"用户姓名和OA账号不能为空，请检查\"}";
                        context.Response.Write(jsonStr);
                        return;
                    }
                    sb.AppendFormat("select fid from tauth where foaid = '{0}'", user1.OaId);
                    if (cm.IsHaveUser(sb))
                    {
                        jsonStr = "{\"err\":-1,\"msg\":\"用户：" + user1.OaId + "已存在，不能重复添加，请修改。\"}";
                        context.Response.Write(jsonStr);
                        return;
                    }
                    sb.Append("insert into tauth");
                    sb.Append(" (fname,foaid,fvariety,funitid,funitcustom,fregdate)");
                    sb.AppendFormat(" values ('{0}','{1}','{2}','{3}','{4}','{5}')", user1.Name, user1.OaId, user1.Variety, user1.UnitId, user1.UnitCustom,DateTime.Now.ToString());
                    if (cm.SqlWithSB(sb) > 0)
                    {
                        jsonStr = "{\"err\":1,\"msg\":\"添加用户成功\"}";
                        context.Response.Write(jsonStr);
                        return;
                    }
                    else
                    {
                        jsonStr = "{\"err\":-1,\"msg\":\"添加用户：失败\"}";
                        context.Response.Write(jsonStr);
                        return;
                    }
                }
                else if (context.Request.QueryString["type"] == "edit")
                {
                    user1.ID = context.Request.Form["fmId"].ToString().Trim();
                    if (user1.Name == "") {
                        jsonStr = "{\"err\":-1,\"msg\":\"用户姓名不能为空，请检查\"}";
                        context.Response.Write(jsonStr);
                        return;
                    }
                    sb.Clear();
                    sb.Append("update tauth");
                    sb.AppendFormat(" set fname = '{0}',fvariety = '{1}',funitid='{2}',funitcustom='{3}'", user1.Name, user1.Variety, user1.UnitId, user1.UnitCustom);
                    sb.AppendFormat(" where fid = '{0}'", user1.ID);
                    if (cm.SqlWithSB(sb) > 0)
                    {
                        jsonStr = "{\"err\":1,\"msg\":\"保存成功\"}";
                        context.Response.Write(jsonStr);
                        return;
                    }
                    else
                    {
                        jsonStr = "{\"err\":-1,\"msg\":\"保存用户信息：失败\"}";
                        context.Response.Write(jsonStr);
                        return;
                    }
                }
            }
            cm.Close();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    public class UserInfo{
        public string ID { set; get; }
        public string Name { set; get; }
        public string OaId { set; get; }
        public string Variety { set; get; }
        public string UnitId { set; get; }
        public string UnitCustom { set; get; }
        public DateTime BirthDay { set; get; }
    }
}